package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UpdateGoods;

/**
 * Servlet implementation class UpdateExecuteServlet
 * 执行对商品信息的修改
 */
public class UpdateExecuteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateExecuteServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		/**
		 * 获取上个页面取得的文本框值，并转码
		 */
		int id = Integer.parseInt(request.getParameter("id"));
		String goodsnameString = new String(request.getParameter("goodsname")
				.getBytes("iso-8859-1"), "UTF-8");
		String goodssortString = new String(request.getParameter("goodssort")
				.getBytes("iso-8859-1"), "UTF-8");
		String goodspriceString = request.getParameter("goodsprice");
		String goodspictureString = new String(request.getParameter(
				"goodspicture").getBytes("iso-8859-1"), "UTF-8");
		String goodsinforString = new String(request.getParameter("goodsinfor")
				.getBytes("iso-8859-1"), "UTF-8");

		if (!UpdateGoods.updateGoods(id, goodsnameString, goodssortString,
				goodspriceString, goodspictureString, goodsinforString)) {//如果修改信息不成功
			request.getRequestDispatcher("/fail.jsp")//跳转失败页面
					.forward(request, response);
			return;
		}
		
		/**
		 * 如果修改成功
		 */
		response.sendRedirect("/CampusTrade/view/detail.do?id="+id);
		return;
	}

}
